using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Service.Interfaces;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.DALProxy.Extensions;

namespace ACME.Northwind.DALProxy
{
	/// <summary>
	/// The service implementation for Order object
	/// </summary>
	public partial class OrderService : IOrderService
	{
		#region IOrderService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public OrderDTO SelectByPrimaryKey(int orderid)
		{
			OrderDTO retVal = new OrderDTO();
			if (!retVal.SelectUsingPK(orderid)) return null;
			else return retVal;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(OrderDTO dto)
		{
			return (dto.Persist() != null);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<OrderDTO> list)
		{
			return list.Persist();
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<OrderDTO> RunSelect()
		{
			List<OrderDTO> retVal = new List<OrderDTO>();
			retVal.RunSelect();
			return retVal;
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<OrderDTO> RunSelect(string linq)
		{
			List<OrderDTO> retVal = new List<OrderDTO>();
			retVal.RunSelect(linq);
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<OrderDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			List<OrderDTO> list = new List<OrderDTO>();
			string sortOrder;
			if (ascending)
				sortOrder = "ascending";
			else
				sortOrder = "descending";
			return list.RunSelect(pageSize.ToString(), pageIndex.ToString(), sortOrder, sortColumn, linq);
		}

		/// <summary>
		/// Return a list of related OrderDetail objects
		/// </summary>
		public List<OrderDetailDTO> GetOrderDetailList(OrderDTO item)
		{
			List<OrderDetailDTO> retval = new List<OrderDetailDTO>();
			retval.SelectByOrder(item.OrderId);
			return retval;
		}

		/// <summary>
		/// Return the related Customer item
		/// </summary>
		public CustomerDTO GetCustomerItem(OrderDTO item)
		{
			CustomerDTO retval = new CustomerDTO();
			retval.SelectUsingPK(item.CustomerId);
			return retval;
		}

		/// <summary>
		/// Return the related Employee item
		/// </summary>
		public EmployeeDTO GetEmployeeItem(OrderDTO item)
		{
			EmployeeDTO retval = new EmployeeDTO();
			retval.SelectUsingPK(item.EmployeeId.Value);
			return retval;
		}

		/// <summary>
		/// Return the related Shipper item
		/// </summary>
		public ShipperDTO GetShipperItem(OrderDTO item)
		{
			ShipperDTO retval = new ShipperDTO();
			retval.SelectUsingPK(item.ShipVia.Value);
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(OrderDTO item)
		{
			Order order = Order.SelectUsingPK(item.OrderId);
			if (order == null) return false;
			order.Delete();
			order.Persist();
			return true;
		}

		#endregion

	}
}
